import TauActionPanel from '@/components/TauActionPanel';
import TauPage from '@/components/TauPage';
import TauTable from '@/components/TauTable';
import { useStore } from '@/hooks';
import { Button, Drawer } from '@alifd/next';
import { t } from '@lingui/macro';
import { uniqueId } from 'lodash';
import { observer } from 'mobx-react-lite';
import React, { useState } from 'react';
import DataForm from './components/DataForm';
import { column } from './stores/definitions';
import { toJS } from 'mobx';

const tableId = uniqueId('tauTable-');

const Log = () => {
  const [modal, setModal] = useState({
    show: null,
    record: null,
  });
  const { logStore: store } = useStore();

  // 获取列表数据
  const handleOnSearch = (params) => {
    const { getOnDate, ...searchArgs } = params.values;
    const [from, to] = getOnDate || [];
    store.search = {
      from,
      to,
      ...searchArgs,
    };
  };

  // 点击打开查看
  const handleOpenView = (record) => {
    setModal({
      show: 'view',
      record,
    });
  };

  // Modal窗口关闭回调
  const handleCancel = () => {
    setModal([]);
  };

  return (
    <TauPage className="page-container">
      <TauActionPanel
        onSearch={handleOnSearch}
        searchItems={[
          {
            name: 'getOnDate',
            type: 'date-range',
            props: {
              placeholder: [t`日志开始时间`, t`日志结束时间`],
            },
          },
        ]}
        keywordSearch={{
          // enabled: true,
          placeholder: t`请输入关键字搜索`,
        }}
        className="action-panel"
      />
      <div className="content">
        <TauTable
          instanceId={tableId}
          primaryKey="id"
          tableLayout="fixed"
          currentPage={store.pg.page}
          pageSize={store.pg.size}
          total={store.total}
          onFetch={(pg) => { store.pg = pg; }}
          loading={store.loading}
          dataSource={toJS(store.logs)}
          columns={[
            { title: t`序号`, dataIndex: 'idx', width: 44, cell: (v, i) => store.idx(i) },
            column('fullname', {}),
            column('title', { width1: 58 }),
            column('remoteAddr', { width1: 58 }),
            column('targetType', { width1: 88 }),
            column('endpoint', {}),
            column('ts', { width1: 68 }),
            {
              title: t`操作`,
              dataIndex: 'channelNumber',
              width: 180,
              cell: (_, __, logs) => (
                <>
                  <Button text type="primary" onClick={() => handleOpenView(logs)} >{t`查看参数`}</Button>
                </>),
            },
          ]}
        />
      </div>
      <Drawer
        visible={!!modal.show}
        title={t`查看用户【${modal.record?.fullname}】日志`}
        style={{ maxWidth: '90%' }}
        closeMode={['esc', 'mask', 'close']}
        onClose={() => setModal({ ...modal, toClose: `toClose$${Date.now()}` })}
        width="600px"
      >
        <DataForm
          modal={modal}
          onCancel={handleCancel}
        />
      </Drawer>
    </TauPage>
  );
};

export default observer(Log);
